package org.example.wumeng.service.impl;

import org.example.wumeng.mapper.T_doctorMapper;
import org.example.wumeng.pojo.Result;
import org.example.wumeng.service.T_doctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.example.wumeng.pojo.Doctor;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class T_doctorServiceImpl implements T_doctorService {

    @Autowired
    private T_doctorMapper doctorMapper;

    @Override
    public Map<String, Object> getList(Integer page, Integer size, String name, String speciality) {
        // 参数验证和默认值设置
        if (page == null || page < 1) {
            page = 1;
        }
        if (size == null || size < 1) {
            size = 10;
        }
        if (size > 100) { // 限制最大查询数量
            size = 100;
        }

        // 计算偏移量
        int offset = (page - 1) * size;

        try {
            // 查询数据
            List<Doctor> doctors = doctorMapper.selectWithCondition(name, speciality, offset, size);
            Long total = doctorMapper.countWithCondition(name, speciality);

            // 构造返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("data", doctors);
            result.put("total", total);
            result.put("page", page);
            result.put("size", size);
            result.put("pages", (total + size - 1) / size); // 计算总页数

            return result;
        } catch (Exception e) {
            throw new RuntimeException("查询医家列表失败: " + e.getMessage());
        }
    }

    @Override
    public Doctor getById(Long id) {
        if (id == null || id <= 0) {
            throw new RuntimeException("医家ID不能为空");
        }

        Doctor doctor = doctorMapper.selectById(id);
        if (doctor == null) {
            throw new RuntimeException("医家信息不存在");
        }

        return doctor;
    }

    @Override
    public Long create(Doctor doctor) {
        // 参数验证
        validateDoctor(doctor);

        // 设置创建和更新时间
        doctor.setCreateTime(LocalDateTime.now());
        doctor.setUpdateTime(LocalDateTime.now());

        try {
            int result = doctorMapper.insert(doctor);
            if (result <= 0) {
                throw new RuntimeException("创建医家失败");
            }
            return doctor.getId();
        } catch (Exception e) {
            throw new RuntimeException("创建医家失败: " + e.getMessage());
        }
    }

    @Override
    public void update(Long id, Doctor doctor) {
        if (id == null || id <= 0) {
            throw new RuntimeException("医家ID不能为空");
        }

        // 检查医家是否存在
        Doctor existDoctor = doctorMapper.selectById(id);
        if (existDoctor == null) {
            throw new RuntimeException("医家信息不存在");
        }

        // 参数验证
        validateDoctor(doctor);

        // 设置ID和更新时间
        doctor.setId(id);
        doctor.setUpdateTime(LocalDateTime.now());
        doctor.setCreateTime(existDoctor.getCreateTime()); // 保持原创建时间

        try {
            int result = doctorMapper.updateById(doctor);
            if (result <= 0) {
                throw new RuntimeException("更新医家失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("更新医家失败: " + e.getMessage());
        }
    }

    @Override
    public void delete(Long id) {
        if (id == null || id <= 0) {
            throw new RuntimeException("医家ID不能为空");
        }

        // 检查医家是否存在
        Doctor existDoctor = doctorMapper.selectById(id);
        if (existDoctor == null) {
            throw new RuntimeException("医家信息不存在");
        }

        try {
            int result = doctorMapper.deleteById(id);
            if (result <= 0) {
                throw new RuntimeException("删除医家失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("删除医家失败: " + e.getMessage());
        }
    }

    @Override
    public Long countBySpeciality(String speciality) {
        if (!StringUtils.hasText(speciality)) {
            return 0L;
        }
        return doctorMapper.countWithCondition(null, speciality);
    }

    /**
     * 验证医家信息
     */
    private void validateDoctor(Doctor doctor) {
        if (doctor == null) {
            throw new RuntimeException("医家信息不能为空");
        }
        if (!StringUtils.hasText(doctor.getName())) {
            throw new RuntimeException("医家姓名不能为空");
        }
        if (!StringUtils.hasText(doctor.getDynasty())) {
            throw new RuntimeException("所属朝代不能为空");
        }
        if (!StringUtils.hasText(doctor.getSpeciality())) {
            throw new RuntimeException("专长领域不能为空");
        }

        // 字段长度验证
        if (doctor.getName().length() > 100) {
            throw new RuntimeException("医家姓名长度不能超过100个字符");
        }
        if (doctor.getDynasty().length() > 50) {
            throw new RuntimeException("朝代长度不能超过50个字符");
        }
        if (doctor.getSpeciality().length() > 200) {
            throw new RuntimeException("专长领域长度不能超过200个字符");
        }
    }

    @Override
    public Result<List<Doctor>> getBySpeciality(String speciality, int page, int size) {
        try {
            int offset = (page - 1) * size;
            List<Doctor> doctors = doctorMapper.selectBySpeciality(speciality, offset, size);
            int total = doctorMapper.countBySpeciality(speciality);

            return Result.success(doctors,(long) total);
        } catch (Exception e) {
            return Result.error("查询专长分类失败: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Map<String, Object>>> getSpecialityStatistics() {
        try {
            List<Map<String, Object>> statistics = doctorMapper.getSpecialityStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            return Result.error("获取专长统计失败: " + e.getMessage());
        }
    }
}
